package net.oschina.app.adapter.move;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.oschina.app.R;
import net.oschina.app.base.BaseListAdapter;
import net.oschina.app.base.Global;
import net.oschina.app.bean2.TweetBean;
import net.oschina.app.protocol.Constant;
import net.oschina.app.ui.activity.move.MoveDetailActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Vashon on 2017/1/16.
 */

public class MoveNewListAdapter extends BaseListAdapter<TweetBean> {
    private View mItemView;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mItemView = LayoutInflater.from(Global.mContext).inflate(R.layout.item_move_new, parent, false);
        MoveNewHolder moveNewHolder = new MoveNewHolder(mItemView);
        return moveNewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TweetBean tweetBean = mList.get(position);
        ((MoveNewHolder)holder).setData(tweetBean);
        ((MoveNewHolder) holder).mTvMoveUsername.setText(tweetBean.getAuthor());
        Spanned spanned = Html.fromHtml(tweetBean.getBody());
        ((MoveNewHolder) holder).mTvMoveContent.setText(spanned);
        ((MoveNewHolder) holder).mTvMoveTime.setText(tweetBean.getPubDate());
        ((MoveNewHolder) holder).mTvCommentCount.setText(Integer.toString(tweetBean.getCommentCount()));
        String imageUrl = tweetBean.getPortrait();
        if (imageUrl != null) {
            imageUrl = imageUrl.replace("192.168.1.107:8080", Constant.GENYMOTION);
            Glide.with(Global.mContext).load(imageUrl).into(((MoveNewHolder) holder).mIvMoveItem);
        }
        if (tweetBean.getLikeCount() != 0 && tweetBean.getLikeCount() < 5) {
            String likeUser = tweetBean.getLikeList().get(0).getName();
            ((MoveNewHolder) holder).mLlAttach.setVisibility(View.VISIBLE);
            for (int i = 1; i < tweetBean.getLikeList().size(); i++) {
                likeUser = likeUser + tweetBean.getLikeList().get(i).getName() + " ";
            }
            likeUser = likeUser + "觉得很赞";
            ((MoveNewHolder) holder).mTvMoveLikeed.setText(likeUser);
        } else if (tweetBean.getLikeCount() != 0) {
            String likeUser = tweetBean.getLikeList().get(0).getName();
            ((MoveNewHolder) holder).mLlAttach.setVisibility(View.VISIBLE);
            for (int i = 1; i < 5; i++) {
                likeUser = likeUser + tweetBean.getLikeList().get(i).getName() + " ";
            }
            likeUser = likeUser + "等" + tweetBean.getLikeCount() + "人觉得很赞";
            ((MoveNewHolder) holder).mTvMoveLikeed.setText(likeUser);
        }
    }

    class MoveNewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_move_item)
        CircleImageView mIvMoveItem;
        @BindView(R.id.tv_move_username)
        TextView mTvMoveUsername;
        @BindView(R.id.tv_move_content)
        TextView mTvMoveContent;
        @BindView(R.id.tv_move_time)
        TextView mTvMoveTime;
        @BindView(R.id.tv_comment_count)
        TextView mTvCommentCount;
        @BindView(R.id.tv_move_likeed)
        TextView mTvMoveLikeed;
        @BindView(R.id.ll_attach)
        LinearLayout mLlAttach;

        MoveNewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(TweetBean tweetBean) {
            final Bundle bundle = new Bundle();
            bundle.putSerializable("data",tweetBean);
            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Global.mContext,MoveDetailActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtras(bundle);
                    Global.mContext.startActivity(intent);
                }
            });
        }
    }
}
